jQuery(document).ready(function()
{
    _init_control();
    _load_manage_temp();

});

function _init_control()
{
    //Template control
    jQuery('#_temp_search').searchbox({  
        searcher:function(value,name){ 
            jQuery("#_manage_template").datagrid("reload");
        },  
        menu:'#_temp_search_menu',  
        prompt:'Please Input Value'  
    });  
    jQuery('#_temp_group').combobox({  
        mulitple: false,
        editable: false,
        panelHeight:100,
        onSelect: function(param){
            jQuery("#_manage_template").datagrid("reload");
        }
    });
    _temp_uploader = new qq.FileUploader({
        element: document.getElementById('_temp_Attach_file'),
        multiple: false, 
        uploadButtonText:"Install template",
        dragText:"Drag Zip file into this area",
        allowedExtensions: ["zip"],
        onSubmit: function(id, name){               
            _temp_uploader.setParams({ 
                ajax_action:1,
                module: "mailsystemmodule",
                action: "templates_manage_save_upload",
            });
        },
        onComplete: function (id, fileName, responseJSON){
            if (responseJSON.success==true)
                {

                $.messager.confirm('Install template successful', 'Are you want to edit?', function(conf){  
                    if (conf){  
                        var url="/index.php?module=mailsystemmodule&action=templates_manage_edit&id="+responseJSON.id;
                        window.location.href=url;
                    }  
                    else
                    {
                       jQuery("#_manage_template").datagrid("reload"); 
                    }

                }); 

            }
            else
                {
                $.messager.alert('Error',responseJSON.error);  
            }
        },
        action: '/index.php'
    });
}
//Template
function _load_manage_temp(){
    jQuery('#_manage_template').datagrid({
        title:'Template manage',
        autoRowHeight: true, 
        idField: "id",
        nowrap: false,
        height: 465,
        pageNumber: 1,
        pageSize: 10,
        pagination: 1,
        rownumbers: true,
        singleSelect: true, 
        columns:[[  

        {field:'name',title:'Name',width:140, sortable:true},  
        {field:'dir',title:'Directory',width:150, sortable:true},
        {field:'group',title:'Group',width:140, sortable:true},
        {field:'description',title:'Description',width:265, sortable:true}, 
        {field:'date_formater',title:'Date',width:160, sortable:true},      

        ]]  ,
        onBeforeLoad: function(param){


            var url="/index.php?module=mailsystemmodule&action=templates_manage_json&ajax_action=1";
            var options=jQuery(this).datagrid("options");

            var search_name=jQuery('#_temp_search').searchbox('getName');
            var search_detail=jQuery('#_temp_search').searchbox('getValue');

            if((search_name!='')&&(search_detail!='')){
                url += "&search_name="+search_name+"&search_detail="+search_detail;
            }

            var _temp_group=jQuery.trim(jQuery("#_temp_group").combobox("getValue"));
            if(_temp_group!='')
                {
                url += "&group="+_temp_group;   
            }


            if (options.sortName != null){
                url+="&sortName="+encodeURI(options.sortName)+"&sortOrder="+options.sortOrder;
            }
            jQuery(this).datagrid('options').url = url;

        }
    }); 
}
//Temp dialog
function _load_manage_temp_edit_dialog(){
    $('#_temp_edit').dialog({
        title:'Template edit',
        width: 800,
        height: 500,
        closed: true,  
        modal: true,
        cache: false,
        buttons:[{
            text:'Ok',
            iconCls:'icon-ok',
            handler:function(){
                jQuery("#_temp_form").submit();              
            }
        },
        {
            text:'Cancel',            
            handler:function(){
                $('#_temp_edit').dialog('close');              
            }
        }]
    });
}

function __temp_Edit(add_new)
{
    var node = jQuery("#_manage_template").datagrid('getSelected');
    var url="/index.php?module=mailsystemmodule&action=templates_manage_edit";
    if(!add_new)
        {
        url+="&id="+node.id;  
    }
    window.location.href=url;
}


function __temp_Edit1(add_new){

    var url="/index.php?module=mailsystemmodule&action=templates_manage_edit&ajax_action=1";

    var node = jQuery("#_manage_template").datagrid('getSelected');
    if (node==null || add_new==true)
        {
        check_profile=true;
        SetWindowPanelTitle("_temp_edit", "Add new template");       
    }
    else
        {
        url+="&id="+node.id;  
        SetWindowPanelTitle("_temp_edit", "View "+node.name+" template");        
    }

    jQuery.ajax({
        url: url,
        success: function(data) {
            jQuery("#_temp_edit_content").html(data);
            jQuery("#_temp_form").submit(function(){
                if (!jQuery(this).valid())
                    {}
                else
                    {

                    // post to server
                    jQuery.ajax({
                        type: 'POST',
                        url: '/index.php',
                        data: jQuery("#_temp_form").serialize(),
                        success: function(data) {                    
                            data = ajax_string_dim(data);
                            if (data == "OK"){
                                _reload_all();
                                jQuery("#_temp_edit").dialog("close");
                            }
                            else{
                                alert("Template save failed.");
                            }
                        },
                        async:false
                    });
                }
                return false;
            });
        },
        async:false
    });

    jQuery('#_temp_edit').dialog("open");

}
//delete
function __temp_Remove()
{
    var node = jQuery("#_manage_template").datagrid('getSelected');
    if(node!=null)
        $.messager.confirm('Delete template', 'Are you sure deleting this item?', function(conf){  
        if (conf){  
            $.get('/index.php?module=mailsystemmodule&ajax_action=1&action=templates_manage_delete&id='+node.id, function(data) {
                if(data=='OK')
                    jQuery("#_manage_template").datagrid("reload");
            }); 
        }  
    });  
}